<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          :disabled="multiple"
          @click="handleCheck"
          size="mini"
          v-hasPermi="['purchase:qgmCheck:check']"
        >
          审核
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleBack"
          v-hasPermi="['purchase:qgmCheck:back']"
        >
          撤销
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['purchase:qgmCheck:export']"
        >
          导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <div
          style="
            border: 1px solid white;
            width: 80px;
            height: 28px;
            line-height: 28px;
            display: flex;
          "
        >
          <div
            style="
              margin-top: 6px;
              width: 15px;
              height: 15px;
              border: 1px solid green;
              background-color: green;
              border-radius: 50%;
              margin-right: 10px;
            "
          ></div>
          <span style="display: block; font-size: 14px">未审核</span>
        </div>
      </el-col>
      <el-col :span="1.5">
        <div
          style="
            border: 1px solid white;
            width: 80px;
            height: 28px;
            line-height: 28px;
            display: flex;
          "
        >
          <div
            style="
              margin-top: 6px;
              width: 15px;
              height: 15px;
              border: 1px solid #bb0331;
              background-color: #bb0331;
              border-radius: 50%;
              margin-right: 10px;
            "
          ></div>
          <span style="display: block; font-size: 14px">已审核</span>
        </div>
      </el-col>
      <el-col :span="2.2" style="position: absolute; right: 0px; top: 0">
        <Page
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="550px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="98px"
      >
        <el-col :span="24" style="margin-top: 10px">
          <el-form-item label="请购单号" prop="qgmDjbh">
            <el-input
              v-model="queryParams.qgmDjbh"
              placeholder="请输入请购单号"
              clearable
              style="width: 300px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="需求日期" prop="dateRange">
            <el-date-picker
              clearable
              size="small"
              v-model="dateRange"
              style="width: 300px"
              :clearable="false"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="建立人员" prop="qgmZtbz">
            <el-input
              v-model="queryParams.qgmJlry"
              placeholder="请输入建立人员"
              clearable
              style="width: 300px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label-width="125px" prop="totalOrder">
            <span slot="label">
              <el-tooltip
                content="综合查询包含物料代码，品名规格，客户编码查询"
                placement="top"
              >
                <i class="el-icon-question"></i>
              </el-tooltip>
              综合查询
            </span>
            <el-input
              v-model="queryParams.totalOrder"
              placeholder="请输入物料代码或品名规格或客户编码"
              clearable
              style="width: 280px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="单据状态" prop="qgmZtbz">
            <el-radio-group v-model="queryParams.qgmZtbz">
              <el-radio
                v-for="dict in ZtbzOptions"
                style="width: 110px"
                :key="dict.dictValue"
                :label="dict.dictValue"
                >{{ dict.dictLabel }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 15px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <!--    头表TAB-->
    <el-table
      highlight-current-row
      v-loading="loading"
      :data="qgmMstrList"
      :height="tableH"
      :cell-style="iCellStyle"
      border
      stripe
      @selection-change="handleSelectionChange"
      @row-click="clickQgdDet"
      ref="somCheckTable"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="请购单号"
        :show-overflow-tooltip="true"
        align="center"
        prop="qgmDjbh"
      />
      <el-table-column
        label="客户"
        :show-overflow-tooltip="true"
        align="center"
        prop="qgmKhdm"
      >
        <template #default="scope">
          <dict-tag :options="customerOptions" :value="scope.row.qgmKhdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="需求日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="qgmXqrq"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.qgmXqrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="单据状态"
        :show-overflow-tooltip="true"
        align="center"
        prop="qgmZtbz"
      >
        <template #default="scope">
          <dict-tag :options="ZtbzOptionss" :value="scope.row.qgmZtbz" />
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        :show-overflow-tooltip="true"
        align="center"
        prop="qgmDesc"
      />
      <el-table-column
        label="建立人员"
        :show-overflow-tooltip="true"
        align="center"
        prop="qgmJlry"
      />
      <el-table-column
        label="建立日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="qgmJlrq"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.qgmJlrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="审核人员"
        :show-overflow-tooltip="true"
        align="center"
        prop="qgmShry"
      />
      <el-table-column
        label="审核日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="qgmShrq"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.qgmShrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 子表TAB-->
    <el-tabs v-model="activeTab">
      <el-tab-pane label="请购明细" name="first">
        <el-table
          v-loading="loading"
          :data="qgdDetList"
          :height="tableHChild"
          border
          stripe
          @selection-change="handleSelectionChange"
        >
          <el-table-column label="#" width="40" align="center" />
          <el-table-column label="销售单号" align="center" prop="qgdSodh" />
          <el-table-column label="物料代码" align="center" prop="qgdWldm" />
          <el-table-column label="代用物料" align="center" prop="qgdDywl" />
          <el-table-column label="品名规格" align="center" prop="itmPmgg" />
          <el-table-column label="采购属性" align="center" prop="qgdCgsx">
            <template #default="scope">
              <dict-tag :options="CgsxOptionss" :value="scope.row.qgdCgsx" />
            </template>
          </el-table-column>
          <el-table-column label="最小包装" align="center" prop="qgdMinbz" />
          <el-table-column label="请购数量" align="center" prop="qgdQgsl" />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="60"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column label="建立人员" align="center" prop="qgdJlry" />
          <el-table-column
            label="建立日期"
            :show-overflow-tooltip="true"
            align="center"
            prop="qgdJlrq"
          >
            <template #default="scope">
              <span>{{ parseTime(scope.row.qgdJlrq, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="物料备注" align="center" prop="qgdDesc" />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="请购统计" name="second">
        <el-table
          v-loading="loading"
          :data="qgdDetCountList"
          border
          stripe
          :height="tableHChild"
          @selection-change="handleSelectionChange"
        >
          <el-table-column label="#" width="40" align="center" />
          <el-table-column label="物料代码" align="center" prop="qgdWldm" />
          <el-table-column label="代用物料" align="center" prop="qgdDywl" />
          <el-table-column label="品名规格" align="center" prop="itmPmgg" />
          <el-table-column label="采购属性" align="center" prop="qgdCgsx">
            <template #default="scope">
              <dict-tag :options="CgsxOptionss" :value="scope.row.qgdCgsx" />
            </template>
          </el-table-column>
          <el-table-column label="最小包装" align="center" prop="qgdMinbz" />
          <el-table-column label="请购数量" align="center" prop="qgdQgsl" />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="60"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import {
  listQgmMstr,
  exportQgmMstr,
  listQgdDet,
  checkQgmMstr,
  revokeQgmMstr,
} from "@/api/purchase/qgmMstr";
import { getAllCustomer } from "@/api/project/customer";
import { getAllCgList } from "@/api/project/mstr";

export default {
  name: "QgmCheck",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      tableH:
        document.body.clientHeight > 900
          ? document.body.clientHeight / 2 - 96
          : document.body.clientHeight / 2 - 108,
      tableHChild: document.body.clientHeight / 2 - 150,
      exportLoading: false,
      //默认子表
      activeTab: "first",
      // 选中数组
      ids: [],
      qgmDjbhs: [],
      selectionList: [],
      childqgmDjbh: null,
      // 日期范围
      dateRange: [
        this.moment().startOf("month").format(),
        this.moment().endOf("month").format(),
      ],
      // 非单个禁用
      single: true,
      //总值
      totalDyl: 1,
      //多选数据
      selectRows: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      // 生产请购单表格数据
      qgmMstrList: [],
      // 生产请购单子项表格数据
      qgdDetList: [],
      qgdDetCountList: [],
      //客户类型
      customerOptions: [],
      //单位字典
      unitFlagOptions: [],
      //采购属性字典‘
      CgsxOptionss: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dylDefultOptions: [
        { dictLabel: "默认代用料", dictValue: "1" },
        { dictLabel: "代用料", dictValue: "0" },
        { dictLabel: "Bom物料", dictValue: "9" },
      ],
      // 状态标志字典
      ZtbzOptions: [],
      ZtbzOptionss: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 30,
        pageType: 2,
        qgmDjbh: null,
        qgmXqrq: null,
        qgmXqrqEnd: null,
        qgmZtbz: "1",
        qgmJlry: null,
        totalOrder: null,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_purchase_state").then((response) => {
      for (let i of response.data) {
        this.ZtbzOptionss.push(i);
      }
      this.ZtbzOptions = response.data;
      const flag = { dictLabel: "新增", dictValue: "0", listClass: "default" };
      this.ZtbzOptionss.push(flag);
    });
    getAllCustomer().then((response) => {
      for (let i of response.data) {
        i.dictLabel = i.cusName;
        i.dictValue = i.cusCode + "";
        i.listClass = "default";
      }
      this.customerOptions = response.data;
    });
    this.getDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
    //将不是字典的集合转为字典
    getAllCgList().then((response) => {
      this.CgsxOptionss = response.data;
      for (let i of this.CgsxOptionss) {
        i.dictLabel = i.cgName;
        i.dictValue = i.cgId + "";
        i.listClass = "default";
      }
    });
  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        qgmId: null,
        qgmDjbh: null,
        qgmKhdm: null,
        qgmXqrq: new Date(),
        qgmZtbz: null,
        qgmDesc: null,
        qgmJlry: null,
        qgmJlrq: null,
        qgmShry: null,
        qgmShrq: null,
      };
      this.resetForm("form");
    },
    //点击单据 查看详细表
    clickQgdDet(row) {
      this.$refs.somCheckTable.toggleRowSelection(row);
      this.childqgmDjbh = row.qgmDjbh;
      listQgdDet(row.qgmDjbh).then((response) => {
        this.qgdDetList = response.data.detail;
        this.qgdDetCountList = response.data.count;
      });
    },
    /** 分页跳转 */
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 查询生产请购单申请列表 */
    getList(row) {
      if (this.dateRange !== null) {
        this.queryParams.qgmXqrq = this.moment(this.dateRange[0]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
        this.queryParams.qgmXqrqEnd = this.moment(this.dateRange[1]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      this.loading = true;
      listQgmMstr(this.queryParams).then((response) => {
        this.qgdDetList = [];
        this.qgdDetCountList = [];
        this.qgmMstrList = response.rows;
        this.total = response.total;
        this.loading = false;
        if (row != null) {
          this.clickQgdDet(row);
        } else if (this.qgmMstrList != null && this.qgmMstrList.length > 0) {
          this.clickQgdDet(this.qgmMstrList[0]);
        }
      });
    },
    iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      //未审核
      if (row.qgmZtbz === 1) {
        return "padding:0px 0;color:green";
      }
      //已审核
      else if (row.qgmZtbz === 2) {
        return "padding:0px 0;color:#bb0331";
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.qgmId);
      this.qgmDjbhs = selection.map((item) => item.qgmDjbh);
      this.selectionList = selection;
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleSearch() {
      this.reset();
      this.title = "查询生产请购单";
      this.openSearch = true;
    },
    /** 审核按钮操作 */
    handleCheck() {
      for (let i of this.selectionList) {
        if (i.qgmZtbz != "1") {
          this.msgError(
            "请购单号: <" + i.qgmDjbh + "> 的单据状态不是未审核,无法进行审核~"
          );
          return;
        }
      }
      this.$confirm("是否确认审核选中的单据?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return checkQgmMstr(this.qgmDjbhs);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("审核成功");
        })
        .catch(() => {});
    },
    /** 撤销按钮操作 */
    handleBack() {
      for (let i of this.selectionList) {
        if (i.qgmZtbz != "2" && i.qgmZtbz != "1") {
          this.msgError("请购单号: <" + i.qgmDjbh + "> 无法撤销~");
          return;
        }
      }
      this.$confirm("是否确认撤回选中的单据?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return revokeQgmMstr(this.qgmDjbhs);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("撤回成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有生产请购单申请数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportQgmMstr(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped></style>
